<?php

class Admin_CategoriesController extends Zend_Controller_Action
{
	private $_auth;
	private $_acl;
    private $_memcache;
    public function init(){
        $acl                = new My_Acl();
        $auth               = $acl->checkAllowed($this->_request);
        $this->_auth        = $auth->getIdentity();
        $this->view->auth   = $this->_auth;
        $this->_acl         = $acl;
        $this->view->acl    = $this->_acl;
        
        $memcache = new Memcache;
        $memcache->connect('localhost', MEMCACHE_PORT) or die ("Could not connect");
        $this->_memcache = $memcache;
    }
    public function indexAction(){
        $modelCategories = new Admin_Model_Categories();
        $categoriesList = $modelCategories->getCategories();
        $this->view->categoriesList = $categoriesList;
        
    }
    public function deleteAction(){
        if($this->getRequest()->isPost()){
            $del = $this->getRequest()->getPost('del');
            if($del == 'Có'){
                $catId = $this->getRequest()->getPost('catId');
                $modelCategories = new Admin_Model_Categories();
                $categoriesCheck = $modelCategories->getDataById($catId);
                $modelCategories->deleteData($catId);
                // ghi log
                if(file_exists(CONFIG_PATH . '/history.ini')){
                    $configHistory = new Zend_Config_Ini(CONFIG_PATH . '/history.ini', APPLICATION_ENV);
                    
                    $dataHistory['userId']      = $this->_auth['userId'];
                    $dataHistory['time']        = time();
                    $dataHistory['url']         = $this->getRequest()->getRequestUri();
                    $dataHistory['module']      = 'admin';
                    $dataHistory['controller']  = 'categories';
                    $dataHistory['action']      = 'delete';
                    $dataHistory['obj']         = 'categories';
                    $dataHistory['objField']    = $categoriesCheck['catName'];
                    $dataHistory['objId']       = $catId;
                    $modelHistory = new Admin_Model_History();
                    $modelHistory->addData($dataHistory);
                }
            }
            $this->_helper->redirector('index', 'categories', 'admin');
        }else{
            $catId = $this->_getParam('catId', 0);
            $modelCategories = new Admin_Model_Categories();
            $this->view->categoriesCheck = $modelCategories->getDataById($catId);
        }
    }
    public function addAction(){
        $form = new Admin_Form_Categories();
        $form->submit->setLabel('Thêm mới');
        $this->view->form = $form;
        $parentId = $this->_getParam('parentId', 0);
        $modelCategories = new Admin_Model_Categories();
        $categoriesList = $modelCategories->getCategories();
        
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $catId          = $form->getValue('catId');
                $parentId       = $form->getValue('parentId');
                $catName        = $form->getValue('catName');
                $ord            = $form->getValue('ord');
                $status         = $form->getValue('status');
				$catId = $modelCategories->addData($catId, $catName, $parentId, $ord, $status);
                // ghi log
                if(file_exists(CONFIG_PATH . '/history.ini')){
                    $configHistory = new Zend_Config_Ini(CONFIG_PATH . '/history.ini', APPLICATION_ENV);
                    
                    $dataHistory['userId']      = $this->_auth['userId'];
                    $dataHistory['time']        = time();
                    $dataHistory['url']         = $this->getRequest()->getRequestUri();
                    $dataHistory['module']      = 'admin';
                    $dataHistory['controller']  = 'categories';
                    $dataHistory['action']      = 'add';
                    $dataHistory['obj']         = 'categories';
                    $dataHistory['objField']    = $catName;
                    $dataHistory['objId']       = $catId;
                    $modelHistory = new Admin_Model_History();
                    $modelHistory->addData($dataHistory);
                }
                $this->_helper->redirector('index', 'categories', 'admin');

            }else{
                $form->populate($formData);
            }
        }
    }
    public function editAction(){
		$form = new Admin_Form_Categories();
		$form->submit->setLabel('Lưu');
		$this->view->form = $form;
		$catId = $this->_getParam('catId', 0);
		if($this->getRequest()->isPost()){
			$formData = $this->getRequest()->getPost();            
			if($form->isValid($formData)){
                $modelCategories = new Admin_Model_Categories();
    			$categoriesCheck = $modelCategories->getDataByID($catId);
                if($categoriesCheck){
                    $dataUpdate['parentId']   = $form->getValue('parentId');
                    $dataUpdate['catName']    = $form->getValue('catName');
                    $dataUpdate['ord']        = $form->getValue('ord');
                    $dataUpdate['status']     = $form->getValue('status');
                    $modelCategories = new Admin_Model_Categories();
                    $modelCategories->updateData($catId, $dataUpdate['catName'], $dataUpdate['parentId'], $dataUpdate['ord'], $dataUpdate['status']);
                    // ghi log
                    if(file_exists(CONFIG_PATH . '/history.ini')){
                        $configHistory = new Zend_Config_Ini(CONFIG_PATH . '/history.ini', APPLICATION_ENV);
                        
                        $dataHistory['userId']      = $this->_auth['userId'];
                        $dataHistory['time']        = time();
                        $dataHistory['url']         = $this->getRequest()->getRequestUri();
                        $dataHistory['module']      = 'admin';
                        $dataHistory['controller']  = 'categories';
                        $dataHistory['action']      = 'edit';
                        $dataHistory['obj']         = 'categories';
                        $dataHistory['objField']    = $dataUpdate['catName'];
                        $dataHistory['objId']       = $catId;
                        $dataHistory['valueField']  = array();
                        $dataHistory['valueOld']    = array();
                        $dataHistory['valueNew']    = array();
                        foreach($dataUpdate as $key=>$value){
                            if(isset($categoriesCheck[$key]) && $value != $categoriesCheck[$key]){
                                $dataHistory['valueField'][] = $key;
                                $dataHistory['valueOld'][] = $categoriesCheck[$key];
                                $dataHistory['valueNew'][] = $value;
                            }
                        }
                        if($dataHistory['valueField']){
                            $dataHistory['valueField']  = json_encode($dataHistory['valueField']);
                            $dataHistory['valueOld']    = json_encode($dataHistory['valueOld']);
                            $dataHistory['valueNew']    = json_encode($dataHistory['valueNew']);
                            $modelHistory = new Admin_Model_History();
                            $modelHistory->addData($dataHistory);
                        }
                    }
                    $this->_helper->redirector('index', 'categories', 'admin');
                }
			}else{
				$form->populate($formData);
			}
		}else{
            $modelCategories = new Admin_Model_Categories();
			$categoriesCheck = $modelCategories->getDataByID($catId)->toArray();
            if($categoriesCheck){
    			$form->populate($categoriesCheck);
            }else{
                $this->_helper->redirector('index', 'categories', 'admin');
            }
		}
    }
}